package cn.itsource.blog.controller;

import cn.itsource.blog.domain. Community;
import cn.itsource.blog.query. CommunityQuery;

import cn.itsource.blog.service.ICommunityService;
import cn.itsource.until.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 参数接口
 * */
@RestController
@RequestMapping("/community")
@Api(tags = "参数接口类") // 用于描述整个API接口的信息，包括API的标题、描述等
public class CommunityController {
    @Autowired
    private ICommunityService communityService;

    /**
     * 查询所有
     * */
    @GetMapping
    @ApiOperation(value = "查询所有") // 用于描述单个接口的操作信息，包括接口的HTTP方法、路径、描述等
    public List< Community> getAll(){
        return  communityService.getAll();
    }

    /**
     * 查询单个
     * */
    @ApiOperation(value = "查询单个")
    @GetMapping("/{id}")
    public  Community getOne(@PathVariable("id")Long id){
        return  communityService.getOne(id);
    }

    /**
     * 删除单个
     * */
    @ApiOperation(value = "删除单个")
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")Long id){
        try {
             communityService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 添加或修改
     * */
    @ApiOperation(value = "添加或修改")
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Community community){
        try {
            if ( community.getId() == null) {
                 communityService.add( community);
            }else {
                 communityService.update( community);}
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    public JsonResult patchDelete(@RequestBody Long[] ids){
        try {
             communityService.patchDelete(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 分页查询+高级查询
     * */
    @ApiOperation(value = "分页查询+高级查询")
    @PostMapping
    public PageInfo< Community> pageQuery(@RequestBody  CommunityQuery query){
        return  communityService.pageQuery(query);
    }


    @Autowired
    private RedisTemplate<String, String> redisTemplate;


////    @GetMapping("/ranking")
//    public List<ZSetOperations.TypedTuple<String>> CommunityRanking() {
//        String zSetKey = "community:ranking"; // ZSET的key
//// 使用reverseRangeWithScores获取分数最高的前十名成员及其分数
//        Set<ZSetOperations.TypedTuple<String>> topCommunities = redisTemplate.opsForZSet().reverseRangeWithScores(zSetKey, 0, 9);
//// 如果需要List而不是Set，可以转换为List
//        return new ArrayList<>(topCommunities);
//    }
//
//    @GetMapping("/communityRanking")
//    public List<Community> getCommunityRanking(){
//        ArrayList<Community> communityList = new ArrayList<>();
//        List<ZSetOperations.TypedTuple<String>> list = CommunityRanking();
//        List<Community> communityList1 = communityService.getAll();
//        for (ZSetOperations.TypedTuple<String> stringTypedTuple : list) {
//            String communityName = stringTypedTuple.getValue();
//            for (Community community : communityList1) {
//                if (communityName.equals(community.getCommunityName())){
//                    communityList.add(community);
//                }
//            }
//
//        }
//        return communityList;

//    }
@GetMapping("/communityRanking")
    public List<Community> getCommunityRanking(){
        return communityService.getCommunityRanking();
    }


}
